package com.example.reference_book.controller;

import com.example.reference_book.entity.Attention;
import com.example.reference_book.entity.Book;
import com.example.reference_book.entity.Course;
import com.example.reference_book.service.AttentionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 关注表(Attention)表控制层
 *
 * @author makejava
 * @since 2022-06-07 22:19:03
 */
@RestController
@RequestMapping("attention")
public class AttentionController {
    /**
     * 服务对象
     */
    @Resource
    private AttentionService attentionService;

    @GetMapping("/searchAttentionData/{id}/{courseId}")
    public ResponseEntity<Boolean> isAttention(@PathVariable("id") Integer id,@PathVariable("courseId") Integer courseId) {
        List<Attention> list=this.attentionService.isAttention(id,courseId);
        if(list.isEmpty()){
            return ResponseEntity.ok(false);
        }
        return ResponseEntity.ok(true);
    }

    /**
     * 分页查询
     *
     * @param attention 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Attention>> queryByPage(Attention attention, PageRequest pageRequest) {
        return ResponseEntity.ok(this.attentionService.queryByPage(attention, pageRequest));
    }

    @GetMapping("/allAttentionData/{id}")
    public ResponseEntity<List<Course>> queryCollect(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.attentionService.queryAttention(id));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Attention> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.attentionService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param attention 实体
     * @return 新增结果
     */
    @PostMapping("/addCourse")
    public ResponseEntity<Attention> add(Attention attention) {
        System.out.println("userid:"+attention.getUserId());
        System.out.println("courseid:"+attention.getCourseId());
        return ResponseEntity.ok(this.attentionService.insert(attention));
    }

    @PostMapping("/deleteCourse")
    public ResponseEntity<Boolean> deleteCourse(Attention attention) {
        return ResponseEntity.ok(this.attentionService.deleteCourse(attention));
    }

    /**
     * 编辑数据
     *
     * @param attention 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Attention> edit(Attention attention) {
        return ResponseEntity.ok(this.attentionService.update(attention));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.attentionService.deleteById(id));
    }

}

